package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.utils.request.RFormParam;
import com.oa.pm.dto.TrainingPlanDTO;
import com.oa.pm.entity.TrainingPlan;
import com.oa.pm.service.ITrainingPlanService;
import com.oa.pm.vo.TrainingPlanListVO;
import com.oa.system.dto.TrainingPlanListDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/trainingPlan")
@Api(description = "培训计划模块")
public class TrainingPlanController extends BaseController {

    private final ITrainingPlanService trainingPlanService;

    @Autowired
    public TrainingPlanController(ITrainingPlanService trainingPlanService) {
        this.trainingPlanService = trainingPlanService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp<PageResult<TrainingPlanListVO>> findList(TrainingPlanListDTO trainingPlanListDTO) {
        return new Resp().success(trainingPlanService.findTrainingPlanList(trainingPlanListDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public void export(TrainingPlanListDTO trainingPlanListDTO) {
        trainingPlanService.export(trainingPlanListDTO, response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @PostMapping("/findById")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<TrainingPlan> findById(@ApiParam(value = "id", required = true) @RequestParam String id) {
        return new Resp().success(trainingPlanService.findTrainingPlanById(id));
    }


    /**
     * 新增
     *
     * @param trainingPlan TrainingPlan
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "培训计划对象") @RequestBody(required = false) TrainingPlanDTO trainingPlan) {
        if (trainingPlanService.addTrainingPlan(trainingPlan)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param trainingPlan TrainingPlan
     * @return Resp
     */
    @PutMapping("/edit")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp edit(@ApiParam(value = "培训计划对象") @RequestBody(required = false) TrainingPlanDTO trainingPlan) {
        if (trainingPlanService.editTrainingPlan(trainingPlan)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param trainingPlan TrainingPlan
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "培训计划对象") @RequestBody(required = false) TrainingPlan trainingPlan) {
        if (trainingPlanService.updateById(trainingPlan)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        TrainingPlan trainingPlan = trainingPlanService.selectById(id);
        if (trainingPlan != null && trainingPlan.getIsDeleted() == 2) {
            trainingPlan.setIsDeleted(1);
            if (trainingPlanService.updateById(trainingPlan)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
